home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Informant Complete 1995 - 2000
/
Delphi Informant Complete 1995 to 2000.iso
/
Delphi Informant Magazine Complete Works SOURCE CODE 1998.rar
/
1998
/
May
/
di9805bt
/
stream
/
STREAMF.PAS
< prev
Wrap
Pascal/Delphi Source File
|
1995-12-27
|
4KB
|
132 lines
unit StreamF;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TStreamForm = class(TForm)
ReadBtn: TButton;
Memo1: TMemo;
MiddleBtn: TButton;
WriteBtn: TButton;
procedure ReadBtnClick(Sender: TObject);
procedure MiddleBtnClick(Sender: TObject);
procedure WriteBtnClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure ShowBuff(const Buff: array of Char;
Count, CurrentPos: LongInt);
end;
var
StreamForm: TStreamForm;
implementation
{$R *.DFM}
procedure TStreamForm.ShowBuff(const Buff: array of Char;
Count, CurrentPos: LongInt);
var
I: Integer;
begin
with Memo1 do
begin
{Convert the buffer from a null terminated
string to a Pascal string and assign it to
the first line of the memo.}
Lines[0] := StrPas(Buff);
{Show the number of bytes read and the current
position of the stream.}
Lines.Add('Bytes Read = ' + IntToStr(Count));
Lines.Add('Stream Position = ' + IntToStr(CurrentPos));
end; {with}
end;
procedure TStreamForm.ReadBtnClick(Sender: TObject);
var
Stream: TFileStream;
Buff: array[0..31] of Char;
Count: LongInt;
begin
{Create a file stream.}
Stream := TFileStream.Create('stream.bin',
fmOpenReadWrite);
try
{Read some bytes from the stream.}
Count := Stream.Read(Buff, 26);
{Put a null at the end of the buffer so you
can treat it as a null terminated string.}
Buff[Count] := #0;
{Display what you have read.}
ShowBuff(Buff, Count, Stream.Position);
finally
Stream.Free;
end; {try}
end;
procedure TStreamForm.MiddleBtnClick(Sender: TObject);
var
Stream: TFileStream;
Buff: array[0..31] of Char;
Count: LongInt;
begin
{Create a file stream.}
Stream := TFileStream.Create('stream.bin',
fmOpenReadWrite);
try
{Move the stream pointer to the location where
you want to begin reading.}
Stream.Seek(10, 0);
{Read some bytes from the stream.}
Count := Stream.Read(Buff, 5);
{Put a null at the end of the buffer so you
can treat it as a null terminated string.}
Buff[Count] := #0;
{Display what you have read.}
ShowBuff(Buff, Count, Stream.Position);
finally
Stream.Free;
end; {try}
end;
procedure TStreamForm.WriteBtnClick(Sender: TObject);
var
InStream,
OutStream: TFileStream;
Buff: array[0..31] of Char;
Count: LongInt;
begin
{Create the input file stream.}
InStream := TFileStream.Create('stream.bin',
fmOpenRead);
{Create the output file stream.}
OutStream := TFileStream.Create('stream.out',
fmCreate);
try
{Move the stream pointer to the location where
you want to begin reading.}
InStream.Seek(10, 0);
{Read some bytes from the stream.}
Count := InStream.Read(Buff, 5);
{Put a null at the end of the buffer so you
can treat it as a null terminated string.}
Buff[Count] := #0;
{Display what you have read.}
ShowBuff(Buff, Count, InStream.Position);
{Write the contents of the buffer to the output
stream.}
OutStream.Write(Buff, Count);
finally
InStream.Free;
OutStream.Free;
end; {try}
end;
end.